package cn.com.platform.web.manage.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.easy.permission.service.system.urlreserve.anotion.UrlReserveAnotion;
import cn.com.easy.permission.web.controller.BaseController;
import cn.com.easy.permission.web.dto.MessageDTO;
import cn.com.easy.permission.web.dto.PageRequestParamDTO;
import cn.com.easy.utils.Page;
import cn.com.easy.utils.PageUtils;
import cn.com.easy.utils.ResponseOutputUtils;
import cn.com.platform.dao.AdvDao;
import cn.com.platform.mybatis.AdvMybatisDao;
import cn.com.platform.web.manage.dto.AdvDto;
import cn.com.platform.web.manage.dto.AdvQueryParamDto;

import com.github.miemiedev.mybatis.paginator.domain.PageList;

/***
 * 广告位管理
 * 
 * @author chenbinghuang 2017年6月14日
 * 
 */
@Controller
@RequestMapping("/manage/adv")
public class AdvManageController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(AdvManageController.class);

	/** 广告位Dao */
	@Autowired
	private AdvDao advDao;

	/** 广告位MybatisDao */
	@Autowired
	private AdvMybatisDao advMybatisDao;

	/***
	 * 广告位管理
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @author chenbinghuang 2017年6月14日
	 */
	@UrlReserveAnotion("广告位管理")
	@RequestMapping("")
	public String index(HttpServletRequest request, HttpServletResponse response) {
		return "/manage/adv/adv";
	}

	/***
	 * 获取广告位列表
	 * 
	 * @param req
	 * @param res
	 * @param paramDto
	 * @param param
	 * @author chenbinghuang 2017年6月14日
	 */
	@UrlReserveAnotion("获取广告位列表")
	@RequestMapping("/list")
	public void getAllAdv(HttpServletRequest req, HttpServletResponse res, PageRequestParamDTO paramDto, final AdvQueryParamDto param) {
		try {
			if (paramDto.getSortOrder() == null) {
				paramDto.setSortName("id");
				paramDto.setSortOrder("desc");
			}
			PageList<AdvDto> pageObject = advMybatisDao.queryAdvs(param, paramDto.buildMyBatisPageRequest());
			Page<AdvDto> page = PageUtils.getPage(pageObject);
			ResponseOutputUtils.renderJson(res, MessageDTO.newInstance("", true, page));
		} catch (Exception ex) {
			logger.error("获取所有内容推荐异常.", ex);
			ResponseOutputUtils.renderJson(res, MessageDTO.newInstance("", false, "获取所有内容推荐异常.ex:" + ex.getLocalizedMessage()));
		}
	}
}
